/*
 * > /*
>  * INSANE - Interactive Structural Analysis Environment
>  *
>  * Copyright (C) 2003-2004
>  * Universidade Federal de Minas Gerais
>  * Escola de Engenharia
>  * Departamento de Engenharia de Estruturas
>  *
>  * Author's email :     insane@dees.ufmg.br
>  * Author's Website :   http://www.dees.ufmg.br/insane
>  *
>  * This program is free software; you can redistribute it and/or
>  * modify it under the terms of the GNU General Public License
>  * as published by the Free Software Foundation; either version 2
>  * of the License, or any later version.
>  *
>  * This program is distributed in the hope that it will be useful,
>  * but WITHOUT ANY WARRANTY; without even the implied warranty of
>  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>  * GNU General Public License for more details.
>  *
>  * You should have received a copy of the GNU General Public License
>  * along with this program; if not, write to the Free Software
>  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
> 1307, USA.
 *
 */

package br.ufmg.dees.insane.ui.rich.postp.postp_view;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.JProgressBar;

/** Class representing the SplashScreen of the application.
* @author Fonseca, Flavio & Pitangueira, Roque
* @since January 2004
*/
public class LoadingSplashScreen extends JProgressBar  {
	
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;

	//Message to be show with the image
	private String message = "Loading Files";
	
	public Toolkit toolkit;
	private static LoadingSplashScreen splash;
	private static final int PROGRESS_HEIGHT = 21;
	private double progress;
	private Image offscreenImg;
	private Graphics offscreenGfx;
	private FontMetrics fm;
	private static DesktopPostp desktop;
	private int progressCont = 100;
	
	//Singleton to guaratee that there will be only one instace in memory
	static {
		splash = new LoadingSplashScreen();
	}
	
//*****************************************************************************
	
	private LoadingSplashScreen()
	{
		setVisible(false);
		toolkit = Toolkit.getDefaultToolkit();
	}
	
//*****************************************************************************
	
	public static LoadingSplashScreen getInstance()
	{
		return splash;
	}
	
//*****************************************************************************
	
	/**
	 * Method initSplash.
	 */
	private void initSplash()
	{
		setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
		setBackground(Color.white);
		
		Font font = new Font("Dialog",Font.PLAIN,10);
		setFont(font);
		fm = getFontMetrics(font);
		
		setSize(200,PROGRESS_HEIGHT);
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension size = getSize();
		
		if (size.width > screenSize.width)
			size.width = screenSize.width;
		
		if (size.height > screenSize.height)
			size.height = screenSize.height;
		
		setVisible(true);
	}
	
//*****************************************************************************
	
	public void openSplash()
	{
		setCursor(new Cursor(Cursor.WAIT_CURSOR));
		initSplash();
	}
	
//*****************************************************************************
	
	public void finish()
	{
		setVisible(false);
		remove(splash);
		splash = new LoadingSplashScreen();
	}
	
//*****************************************************************************
	
	public synchronized void advance() {
		progress+=1;
		repaint();
		// wait for it to be painted to ensure progress is updated continuously
		try	{
			wait();
		}
		catch(InterruptedException ie) {}
	}
		
//*****************************************************************************
		
	public void update(Graphics g) {
		paint(g);
	}
	
//*****************************************************************************
	
	public synchronized void paint(Graphics g) {
		Dimension size = getSize();
		
		
		if(offscreenImg == null) {
			offscreenImg = createImage(size.width,size.height);
			offscreenGfx = offscreenImg.getGraphics();
			offscreenGfx.setFont(getFont());
		}
		
		offscreenGfx.setColor(Color.gray);
//		offscreenGfx.drawRect(0,0,size.width -2,size.height-2);
		offscreenGfx.drawRect(0,0,size.width -2,PROGRESS_HEIGHT+1);
		
		offscreenGfx.drawImage(null,1,1,this);
		
		// Obs: This should not be hardcoded
		offscreenGfx.setColor(new Color(168,173,189));
		offscreenGfx.fillRect(1,1, (int)((this.getWidth() - 2) * progress) / progressCont,PROGRESS_HEIGHT);
		
		offscreenGfx.setColor(Color.BLACK);
		offscreenGfx.drawString(message, (getWidth() - fm.stringWidth(message)) / 2, PROGRESS_HEIGHT - fm.getDescent() -5);
		
		g.drawImage(offscreenImg,0,0,this);
		
		notify();
		
	}

	/**
	 * The method return the desktop.
	 * @return Returns The desktop.
	 */
	public DesktopPostp getDesktop() {
		return desktop;
	}

	/**
	 * The method set the field desktop from the parameter desktop.
	 * @param desktop The desktop to set.
	 */
	public static  void setDesktop(DesktopPostp desk) {
		desktop = desk;
	}

	/**
	 * The method return the progressCont.
	 * @return Returns The progressCont.
	 */
	public int getProgressCont() {
		return progressCont;
	}

	/**
	 * The method set the field progressCont from the parameter progressCont.
	 * @param progressCont The progressCont to set.
	 */
	public void setProgressCont(int progressCont) {
		this.progressCont = progressCont;
	}
	
	
	
	
//*****************************************************************************
}



